<template>
  <div class="mainua">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>SKU编辑</span>
          </div>
        </div>
        <div class="main_tile boxdq marginTop10">
          <div class="main_tile_l"><em></em>基本信息</div>
        </div>
        <div class="mainua_form">
          <el-form
            v-loading="loading"
            element-loading-text="加载中"
            :inline="false"
            :rules="rules"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="110px"
          >
            <el-form-item label="商品名称：" prop="">
              <el-input
                v-model.trim="info.goods_name"
                placeholder="商品名称："
                clearable
                class="wid240"
                disabled
              ></el-input>
            </el-form-item>
            <el-form-item label="规格：" prop="spu_name">
              <el-select
                v-model.trim="form.spu_name"
                placeholder="规格"
                class="wid240"
                v-if="info.transfer == 0"
                :disabled="true"
              >
                <el-option
                  v-for="(attribute, index) in info.attribute"
                  :key="index"
                  :label="attribute.title"
                  :value="attribute.id"
                ></el-option>
              </el-select>
              <el-select
                v-model.trim="form.spu_name"
                placeholder="规格"
                class="wid240"
                v-if="info.transfer == 1"
                :disabled="true"
              >
                <el-option
                  v-for="(unit, index) in info.unit"
                  :key="index"
                  :label="unit.title"
                  :value="unit.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="单位：" prop="unit">
              <el-select
                v-model.trim="form.unit"
                placeholder="单位"
                class="wid240"
                :disabled="true"
              >
                <el-option
                  v-for="(unit, index) in info.unit"
                  :key="index"
                  :label="unit.title"
                  :value="unit.id"
                ></el-option>
              </el-select>
            </el-form-item>

            <!-- <el-form-item label="排序：" prop="sort">
              <el-input
                v-model.trim="form.sort"
                placeholder="排序"
                clearable
                class="wid240"
              ></el-input>
            </el-form-item> -->
            <el-form-item label="编码sku：" prop="goods_spu_code">
              <el-input
                v-model.trim="form.goods_spu_code"
                placeholder="编码sku"
                clearable
                class="wid240"
                @change="isEdit = true"
              ></el-input>
            </el-form-item>
            <!-- is_update: 产生数据不能编辑互调比 -->
            <el-form-item label="互调比：" prop="package_num">
              <el-input
                v-model.trim="form.package_num"
                placeholder="互调比"
                clearable
                class="wid240"
                :disabled="(info.transfer == 1 ? false : true) || info.is_update=='no'"
                @change="isEdit = true"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item label="采购价：" prop="market_price">
              <el-input
                v-model.trim="form.market_price"
                placeholder="采购价"
                clearable
                class="wid240"
              ></el-input>

            </el-form-item> -->
            <el-form-item label="市场价：" prop="market_price">
              <el-input
                v-model.trim="form.market_price"
                placeholder="市场价"
                clearable
                class="wid240"
                @change="isEdit = true"
              ></el-input>
            </el-form-item>

            <el-form-item label="损耗率：" prop="attrition_rate">
              <el-input
                v-model.trim="form.attrition_rate"
                placeholder="请输入1-100以内的数值"
                clearable
                class="wid240"
                @change="isEdit = true"
              ></el-input>
            </el-form-item>
            <el-form-item label="起订量：" prop="">
              <el-input
                v-model.trim="form.minimum"
                placeholder="起订量"
                clearable
                class="wid240"
                @change="isEdit = true"
              ></el-input>
            </el-form-item>

            <el-form-item label="指定库存：">
              <div class="form_flex">
                <el-radio-group v-model="form.designated_inventory">
                  <el-radio :label="0">不指定库存</el-radio>
                  <el-radio :label="-1">指定实际库存</el-radio>
                  <el-radio :label="form.demo_designated_inventory"
                    >自定义库存</el-radio
                  >
                </el-radio-group>
                <el-input
                  v-show="
                    form.designated_inventory !== 0 &&
                    form.designated_inventory !== -1
                  "
                  onkeyup="value=value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')"
                  @input="edit_input"
                  @blur="blur_input"
                  v-model.trim="form.demo_designated_inventory"
                  placeholder="请输入大于0的数值"
                  clearable
                  class="wid_240"
                  @change="isEdit = true"
                ></el-input>
              </div>
            </el-form-item>

            <el-form-item label="折扣：" prop="discount">
              <el-input
                v-model.trim="form.discount"
                placeholder="请输入0.001-1以内的数值"
                clearable
                class="wid240"
                @change="isEdit = true"
              ></el-input>
              <el-button type="primary" @click="toDiscounts">用户专属折扣</el-button>
            </el-form-item>

            <el-form-item label="分拣宽容度：" prop="tolerance">
              <el-input
                v-model.trim="form.tolerance"
                placeholder="请输入1-100以内的数值"
                clearable
                class="wid240"
              ></el-input>
              <span class="tips">设置宽容度后,分拣数量上、下不能超过比例</span>
            </el-form-item>
            
            <el-form-item label="描述：" prop="describe">
              <el-input
                type="textarea"
                v-model.trim="form.describe"
                placeholder="描述"
                clearable
                style="width: 30%"
                @change="isEdit = true"
              ></el-input>
            </el-form-item>
            <div class="box">
              <el-form-item label="售卖状态：" prop="status">
                <el-switch
                  v-model="form.status"
                  active-color="#03A98E"
                  active-text="开"
                  inactive-text="关"
                  class="mainor_switch"
                  @change="isEdit = true"
                ></el-switch>
              </el-form-item>
              <!-- <el-form-item label="分拣转换：" prop="is_transfer">
                <el-switch
                  v-model="form.is_transfer"
                  active-color="#03A98E"
                  active-text="开"
                  inactive-text="关"
                  class="mainor_switch"
                ></el-switch>
              </el-form-item> -->
              <!-- <el-form-item label="小数加购：" prop="is_float">
                <el-switch
                  v-model="form.is_float"
                  active-color="#03A98E"
                  active-text="开"
                  inactive-text="关"
                  class="mainor_switch"
                ></el-switch>
              </el-form-item> -->
              <el-form-item label="时价：" prop="current_price">
                <el-switch
                  v-model="form.current_price"
                  active-color="#03A98E"
                  active-text="开"
                  inactive-text="关"
                  class="mainor_switch"
                ></el-switch>
              </el-form-item>
            </div>
            <!-- <div class="maincla_zh box">
              <span>1{{ form.unit | unit(info) }}约</span>
              <el-form-item prop="transfer_num" label-width="10px" label=" ">
                <el-input v-model.trim="form.transfer_num" placeholder clearable class="wid180"></el-input>
              </el-form-item>
              <el-form-item prop="transfer_unit" label-width="10px" label=" ">
                <el-select v-model.trim="form.transfer_unit" placeholder="单位" class="wid150">
                  <el-option
                    v-for="(unit,index) in info.unit"
                    :key="index"
                    :label="unit.title"
                    :value="unit.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div> -->
          </el-form>
          <div class="clearfix"></div>
          <div class="newConBox_p100 paddingBottom10" id="newConBox">
            <div class="newConBox_h32">
              <div
                :class="[
                  'mainua_an newConBox ',
                  { on: searchBarFixed },
                  { marginLeft5: !searchBarFixed },
                ]"
              >
                <el-button type="primary" @click="getArticleList" v-repeatClick
                  >保存</el-button
                >
                <el-button @click="$router.back(-1)">取消</el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { sku_info, sku_edit } from "@/api/commodity/commodity_Pricing";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import rules from "@/rules/commodity/commodityPricing_edit";

export default {
  components: {},
  filters: {
    unit(id, arry) {
      if (arry.unit) {
        let name = "";
        arry.unit.forEach((e) => {
          if (e.id == id) {
            name = e.title;
          }
        });
        return name;
      }
    },
  },

  data() {
    return {
      // 验证
      rules,
      // 搜索
      form: {
        sort: 0,
        spu_name: "",
        unit: "",
        package_num: "",
        goods_spu_code: "",
        market_price: "",
        attrition_rate: "",
        minimum: "",
        designated_inventory: "",
        status: true,
        is_transfer: true,
        is_float: true,
        current_price: true,
        transfer_num: "",
        transfer_unit: "",
        describe: "",
        discount: "",
        tolerance: "",
        demo_designated_inventory: 0,
      },
      //信息
      info: {
        goods_name: "",
        transfer: 1,
      },
      // id
      id: this.$route.params.id,
      // 监听滚动
      searchBarFixed: false,
      isEdit: false,
      loading: false,
    };
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  beforeRouteLeave(to, from, next) {
    if (this.isEdit) {
      setTimeout(() => {
        this.$confirm(
          "您已经有数据操作，离开页面系统不会保存您的操作，确定离开吗？",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            // 正常跳转
            next();
          })
          .catch(() => {
            next(false);
          });
      }, 200);
    } else {
      next();
    }
  },
  created() {
    // 基本信息
    this.sku_info();
  },
  methods: {
    edit_input(val) {
      this.form.designated_inventory = val;
    },
    blur_input(event) {
      if (event.target.value < 0.1) {
        event.target.value = 1;
      }
    },
    // 监听滚动
    handleScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop + document.documentElement.clientHeight >
      document.getElementById("newConBox").offsetTop + 200
        ? (this.searchBarFixed = false)
        : (this.searchBarFixed = true);
    },
    // 保存
    getArticleList() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          this.isEdit = false;
          delete this.form.demo_designated_inventory;
          let form = JSON.parse(JSON.stringify(this.form));
          form.transfer_unit = form.transfer_unit ? form.transfer_unit : 0;
          form.status = form.status ? 1 : 0;
          form.is_transfer = form.is_transfer ? 1 : 0;
          form.is_float = form.is_float ? 1 : 0;
          form.current_price = form.current_price ? 1 : 0;
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
                  apiUrl().appsecret
              ),
            },
            apiUrl().name,
            { id: this.id },
            form
          );
          sku_edit(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "编辑成功",
                type: "success",
              });

              this.$router.go(-1);
            } else {
              this.$message.error(res.errmsg);
            }
          });
        }
      });
    },
    // 基本信息
    sku_info() {
      this.loading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      sku_info(data).then((res) => {
        this.loading = false;
        if (res.errcode == 0) {
          this.form.spu_name = parseInt(res.data.info.spu_name);
          this.form.unit = parseInt(res.data.info.unit);
          this.form.transfer_unit = parseInt(res.data.info.transfer_unit)
            ? parseInt(res.data.info.transfer_unit)
            : "";
          this.form.package_num = res.data.info.package_num;
          this.form.sort = res.data.info.sort;
          this.form.goods_spu_code = res.data.info.goods_spu_code;
          this.form.market_price = res.data.info.market_price;
          this.form.attrition_rate = res.data.info.attrition_rate;
          this.form.minimum = res.data.info.minimum;
          this.form.designated_inventory = res.data.info.designated_inventory;
          if (this.form.designated_inventory > 0) {
            this.form.demo_designated_inventory =
              this.form.designated_inventory;
          } else {
            this.form.demo_designated_inventory = 1;
          }
          this.form.status = res.data.info.status == 1 ? true : false;
          this.form.is_transfer = res.data.info.is_transfer == 1 ? true : false;
          this.form.is_float = res.data.info.is_float == 1 ? true : false;
          this.form.current_price =
            res.data.info.current_price == 1 ? true : false;
          this.form.transfer_num = res.data.info.transfer_num;
          this.form.describe = res.data.info.describe;
          this.form.discount = res.data.info.discount;
          this.form.tolerance = res.data.info.tolerance;
          this.info = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 去用户专属折扣
    toDiscounts() {
      this.$router.push(`/commodity/pricing/userDiscounts/${this.id}`);
    }
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
.tips {
  color: #e47c02;
  font-size: 14px;
  margin-left: 10px;
}
.mainua_form {
  margin-left: 10px;
  margin-top: 10px;
  /deep/.el-form-item--mini.el-form-item,
  /deep/.el-form-item--small.el-form-item {
    margin-bottom: 22px;
  }
  .mainua_an {
    /deep/.el-form-item__content {
      margin-left: 0px !important;
    }
    .reset {
      background: $subActive;
      color: $menuText;
      border-color: $subActive;

      &:hover,
      &:focus {
        border-color: $menuActiveText;
        color: $menuActiveText;
      }
    }
  }
  .maincla_zh {
    margin-left: 107px;
    align-items: center;
    /deep/.el-form-item--mini.el-form-item,
    /deep/.el-form-item--small.el-form-item {
      margin-bottom: 0;
    }
    & > span {
      margin-right: 5px;
    }
    /deep/.el-select--small {
      margin-left: 5px;
    }
  }
}
.form_flex {
  display: flex;
  .wid_240{
    width: 240px;
    margin-left: 20px;
  }
  .el-radio-group {
    margin-top: 9px;
    /deep/.el-radio__inner {
      border: 1px solid #555;
    }
    /deep/.el-radio__inner::after {
      width: 8px;
      height: 8px;
      left: 7px;
      top: 7px;
    }
    /deep/.el-radio {
      font-weight: 400;
    }
  }
  .wid240 {
    margin-top: 18px;
  }
}
</style>
